//
//  CoreDatePickerView.m
//  mobile
//
//  Created by abc on 2018/1/31.
//  Copyright © 2018年 azazie. All rights reserved.
//

#import "CoreDatePickerView.h"

#import <base/base.h>
#import <QMUIKit/QMUIKit.h>

@interface CoreDatePickerView ()
{
    NSString *_title;
    BOOL _isAutoSelect;
    UIDatePickerMode _dateType;
    
    NSDate *_minDate;
    NSDate *_maxDate;
    NSDate *_selectValue;
    
    DateDisplayType _displayType;
    NSString * _format;
    UIDatePickerMode _mode;
    NSDateFormatterStyle _style;
}

@property (nonatomic, strong) UIDatePicker *datePicker;

@end

@implementation CoreDatePickerView

#pragma mark - 显示时间选择器
+ (CoreDatePickerView *)datePickerViewWithTitle:(NSString *)title
                                       dateType:(UIDatePickerMode)type
                                     clearBlock:(ClearBlock)clearBlock
                                    resultBlock:(ResultBlock)resultBlock
{
    CoreDatePickerView *datePickerView = [[CoreDatePickerView alloc] initWithTitle:title
                                                                          dateType:type
                                                                        clearBlock:clearBlock
                                                                       resultBlock:resultBlock];
    return datePickerView;
}

#pragma mark - 初始化时间选择器
- (instancetype)initWithTitle:(NSString *)title
                     dateType:(UIDatePickerMode)type
                   clearBlock:(ClearBlock)clearBlock
                  resultBlock:(ResultBlock)resultBlock {
    if (self = [super init]) {
        _title = title;
        _dateType = type;
        
        _minDate = nil;
        _maxDate = nil;
        _selectValue = [NSDate date];
        
        _isAutoSelect = YES;
        
        _displayType = DateDisplayType_Format;
        _format = @"yyyy-MM-dd";
        
        self.clearBlock = clearBlock;
        self.resultBlock = resultBlock;
        
        @weakify(self);
        self.dismissBlock = ^{
            @strongify(self);
            [self.datePicker removeFromSuperview];
            self.datePicker = nil;
        };
        
        [self initUI];
    }
    return self;
}

#pragma mark - 初始化子视图
- (void)initUI {
    [super initUI];
    self.titleLabel.text = _title;
    [self.alertView addSubview:self.datePicker];
}

#pragma mark - 时间选择器
- (UIDatePicker *)datePicker {
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, kTopViewHeight + 0.5, SCREEN_WIDTH, kDatePicHeight)];
        if (@available(iOS 13.4, *)) {
            [_datePicker setPreferredDatePickerStyle:UIDatePickerStyleWheels];
        } else {
            // Fallback on earlier versions
        };
        _datePicker.backgroundColor = [UIColor whiteColor];
        _datePicker.tintColor = [UIColor clearColor];
        _datePicker.datePickerMode = _dateType;
        _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        if (_minDate) {
            _datePicker.minimumDate = _minDate;
        }
        if (_maxDate) {
            _datePicker.maximumDate = _maxDate;
        }
        
        [_datePicker setDate:_selectValue animated:YES];
        
        [_datePicker addTarget:self action:@selector(didSelectValueChanged:) forControlEvents:UIControlEventValueChanged];
    }
    return _datePicker;
}

#pragma mark - background view click event
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [self dismissWithAnimation:NO];
}

#pragma mark - 时间选择器的滚动响应事件
- (void)didSelectValueChanged:(UIDatePicker *)sender {
    _selectValue = sender.date;
    if (_isAutoSelect) {
        if (self.resultBlock) {
            self.resultBlock([self stringForDate:_selectValue]);
        }
    }
}

#pragma mark - 取消按钮的点击事件
- (void)clickLeftBtn {
    [self dismissWithAnimation:YES];
    if (self.clearBlock) {
        self.clearBlock();
    }
}

#pragma mark - 确定按钮的点击事件
- (void)clickRightBtn {
    [self dismissWithAnimation:YES];
    if (self.resultBlock) {
        self.resultBlock([self stringForDate:_selectValue]);
    }
}

#pragma mark - 格式转换
- (NSDate *)dateForString:(NSString *)string {
    NSDate *date = nil;
    switch (_displayType) {
        case DateDisplayType_Style:
            date = [DateFormatManger dateOfString:string forStyle:_style];
            break;
        case DateDisplayType_Format:
            date = [DateFormatManger dateOfString:string forFormat:_format];
            break;
        case DateDisplayType_Mode:
            date = [DateFormatManger dateOfString:string forMode:_mode];
            break;
        default:
            break;
    }
    return date;
}

- (NSString *)stringForDate:(NSDate *)date {
    NSString *string = nil;
    switch (_displayType) {
        case DateDisplayType_Style:
            string = [DateFormatManger stringOfDate:date forStyle:_style];
            break;
        case DateDisplayType_Format:
            string = [DateFormatManger stringOfDate:date forFormat:_format];
            break;
        case DateDisplayType_Mode:
            string = [DateFormatManger stringOfDate:date forMode:_mode];
            break;
        default:
            break;
    }
    return string;
}

#pragma mark - 属性定制
- (CoreDatePickerView *(^)(NSDate *date))minDate {
    return ^(NSDate *date) {
        self.datePicker.minimumDate = date;
        return self;
    };
}
- (CoreDatePickerView *(^)(NSDate *date))maxDate {
    return ^(NSDate *date) {
        self.datePicker.maximumDate = date;
        return self;
    };
}
- (CoreDatePickerView *(^)(NSDate *date))defaultSelDate {
    return ^(NSDate *date) {
        _selectValue = date ? : [NSDate date];
        [self.datePicker setDate:_selectValue animated:YES];
        return self;
    };
}
- (CoreDatePickerView *(^)(NSString *value))defaultSelValue {
    return ^(NSString *value) {
        _selectValue = [self dateForString:value] ? : [NSDate date];
        [self.datePicker setDate:_selectValue animated:YES];
        return self;
    };
}
- (CoreDatePickerView *(^)(BOOL autoSelect))autoSelect {
    return ^(BOOL autoSelect) {
        _isAutoSelect = autoSelect;
        return self;
    };
}
- (CoreDatePickerView *(^)(NSString *displatFormat))displatFormat {
    return ^(NSString *displatFormat) {
        _displayType = DateDisplayType_Format;
        _format = displatFormat;
        return self;
    };
}
- (CoreDatePickerView *(^)(UIDatePickerMode displayMode))displayMode {
    return ^(UIDatePickerMode displayMode) {
        _displayType = DateDisplayType_Mode;
        _mode = displayMode;
        return self;
    };
}
- (CoreDatePickerView *(^)(NSDateFormatterStyle displatStyle))displatStyle {
    return ^(NSDateFormatterStyle displatStyle) {
        _displayType = DateDisplayType_Style;
        _style = displatStyle;
        return self;
    };
}
- (CoreDatePickerView *(^)())show {
    return ^() {
        [self showWithAnimation:YES];
        return self;
    };
}

@end
